/*
Copyright (c) 2013, Sten Lindgren
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef LEXER_H_
#define LEXER_H_


#define TOKEN_MAX	80	/* max length of strings in request */


typedef enum	{
		TOKEN_TYPE_NONE = 0,			/* types known to parser */
		TOKEN_TYPE_CHAR,			/* unknown char */
		TOKEN_TYPE_END_OF_LINE,			/* line ending */
		TOKEN_TYPE_NUMBER,			/* number */
		TOKEN_TYPE_STRING,			/* string delimited by " or ' */
		TOKEN_TYPE_IDENT			/* identifier name */
		} token_type_t;

typedef enum	{
		TOKEN_IDENT_NONE = 0,			/* special identifier names */
		TOKEN_IDENT_BELL,
		TOKEN_IDENT_DIM,
		TOKEN_IDENT_EXIT,
		TOKEN_IDENT_EVENTS,
		TOKEN_IDENT_OFF,
		TOKEN_IDENT_ON
		} token_ident_t;

typedef struct	{
		token_type_t	type;			/* token type */
		token_ident_t	ident;			/* token identifier */
		char		chars[TOKEN_MAX + 1];	/* chars used to parse token */
		int		number;			/* numeric representation of token */
		} token_t;


extern char	next_token_char;	/* next char to parse */
extern token_t	next_token;		/* description of next token during parse */

extern char	*token_type_name[];


/* Get next char from socket, to be used only by tokenizer.
   Freeze input at end if line. */

extern void parse_next_char(void);


/* force consuming input at end of line */

extern void parse_skip_end_of_line(void);


/* parse a number, assuming first char is already verified to be a digit */

extern void parse_next_token_number(void);


/* parse string, assuming first char is already verified to be a delimiter */

extern void parse_next_token_string(void);


/* parse identifier, assuming first char is already verified to be valid */

extern void parse_next_token_ident(void);


/* parse single character not matching any other token type */

extern void parse_next_token_char(void);


/* parse next token and leave result in next_token struct */

extern void parse_next_token(void);


/* prepare parser and get first token */

extern void parse_start(int sock);

#endif  /* LEXER_H_ */
